package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DishController {
	@Autowired
	private DishService dishService;
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@GetMapping("/dish/list")
	public ResultInfo getCategory(Long categoryId, Long status) {
		//设计一个键dish_categoryID
		String key = "dish_" + categoryId;
		
		//查询先从redis中查询
		List<Dish> dishList = (List<Dish>)redisTemplate.opsForValue().get(key);
		if (CollectionUtil.isNotEmpty(dishList)) {
			System.out.println("从Redis中查询到的");
			return ResultInfo.success(dishList);
		} else {
			//Redis中没有查到数据
			//redis中查询不到再从数据库中查到Redis后返回到前端.
			List<Dish> categoryList = dishService.findByCategoryId(categoryId,status);
			//先存储到redis服务器中
			redisTemplate.opsForValue().set(key,categoryList);
			//再返回
			return ResultInfo.success(categoryList);
		}
		
		
	}
}
